package com.atguigu.gmall.controller;

import com.atguigu.gmall.bean.*;
import com.atguigu.gmall.service.TradeStatsService;
import com.atguigu.gmall.util.DateFormatUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Felix
 * @date 2024/11/11
 * 交易域统计controller
 * @Controller      :将对象的创建交给容器，如果方法的返回值是String，会进行页面的跳转，如果不想进行跳转，可以加@ResponseBody
 * @RestController  :将对象的创建交给容器，如果方法的返回值是String，不会进行页面的跳转，直接将字符串返回给客户端
 * @RestController = @Controller  + @ResponseBody
 * @RequestMapping("/gmv") :拦截请求，将请求交给标注的方法进行处理
 */
@RestController
public class TradeStatsController {
    @Autowired
    private TradeStatsService tradeStatsService;


    @RequestMapping("/province")
    public Map getProvinceTransaction(@RequestParam(value = "date",defaultValue = "0") Integer date){
        if(date == 0){
            date = DateFormatUtil.now();
        }
        List<TradeProvinceTransaction> provinceTransactionList = tradeStatsService.getProvinceTransaction(date);

        Map reMap = new HashMap();
        reMap.put("status",0);

        Map mapData = new HashMap();

        List mapDataList = new ArrayList();
        for (TradeProvinceTransaction pt : provinceTransactionList) {
            Map map = new HashMap();
            map.put("name",pt.getProvince());
            map.put("value",pt.getOrderAmount());
            map.put("sizeValue",pt.getOrderUser());
            map.put("type",pt.getOrderCount());
            mapDataList.add(map);
        }
        mapData.put("mapData",mapDataList);
        mapData.put("valueName","交易额");
        mapData.put("sizeValueName","下单人数");
        mapData.put("typeName","下单次数");

        reMap.put("data",mapData);

        return reMap;
    }

    @RequestMapping("/source1")
    public String getSourceTransaction1(@RequestParam(value = "date",defaultValue = "0") Integer date){
        if (date == 0){
            date = DateFormatUtil.now();
        }
        List<SourceTransaction> sourceTransaction = tradeStatsService.getSourceTransaction(date);
        StringBuilder rs = new StringBuilder("{\"status\": 0,\"msg\": \"\",\"data\": ");
        for (int i = 0; i < sourceTransaction.size(); i++) {
            SourceTransaction st = sourceTransaction.get(i);
            if (st.getSc().equals("360")){
                rs.append(st.getOrderAmount());
            }
        }
        rs.append("}");

        return rs.toString();
    }

    @RequestMapping("/source2")
    public String getSourceTransaction2(@RequestParam(value = "date",defaultValue = "0") Integer date){
        if (date == 0){
            date = DateFormatUtil.now();
        }
        List<SourceTransaction> sourceTransaction = tradeStatsService.getSourceTransaction(date);
        StringBuilder rs = new StringBuilder("{\"status\": 0,\"msg\": \"\",\"data\": ");
        for (int i = 0; i < sourceTransaction.size(); i++) {
            SourceTransaction st = sourceTransaction.get(i);
            if (st.getSc().equals("baidu")){
                rs.append(st.getOrderAmount());
            }
        }
        rs.append("}");

        return rs.toString();
    }

    @RequestMapping("/source3")
    public String getSourceTransaction3(@RequestParam(value = "date",defaultValue = "0") Integer date){
        if (date == 0){
            date = DateFormatUtil.now();
        }
        List<SourceTransaction> sourceTransaction = tradeStatsService.getSourceTransaction(date);
        StringBuilder rs = new StringBuilder("{\"status\": 0,\"msg\": \"\",\"data\": ");
        for (int i = 0; i < sourceTransaction.size(); i++) {
            SourceTransaction st = sourceTransaction.get(i);
            if (st.getSc().equals("tiktok")){
                rs.append(st.getOrderAmount());
            }
        }
        rs.append("}");

        return rs.toString();
    }

    @RequestMapping("/source4")
    public String getSourceTransaction4(@RequestParam(value = "date",defaultValue = "0") Integer date){
        if (date == 0){
            date = DateFormatUtil.now();
        }
        List<SourceTransaction> sourceTransaction = tradeStatsService.getSourceTransaction(date);
        StringBuilder rs = new StringBuilder("{\"status\": 0,\"msg\": \"\",\"data\": ");
        for (int i = 0; i < sourceTransaction.size(); i++) {
            SourceTransaction st = sourceTransaction.get(i);
            if (st.getSc().equals("toutiao")){
                rs.append(st.getOrderAmount());
            }
        }
        rs.append("}");

        return rs.toString();
    }

    @RequestMapping("/statistics")
    public String getTransactionStatistics(@RequestParam(value = "date",defaultValue = "0") Integer date){
        if (date == 0){
            date = DateFormatUtil.now();
        }
        List<TransactionStatistics> transactionStatistics = tradeStatsService.getTransactionStatistics(date);
        TransactionStatistics ts = transactionStatistics.get(0);

        StringBuilder rs = new StringBuilder("{\n" +
                "  \"status\": 0,\n" +
                "  \"msg\": \"\",\n" +
                "  \"data\": [\n" +
                "    {\n" +
                "      \"name\": \"下单用户\",\n" +
                "      \"desc\": \"下单用户是指用户下单的次数\",\"value\": ");
        rs.append(ts.getOrderUser());
        rs.append(",\"rate_level\": \"green\"\n" +
                "    },\n" +
                "    {\n" +
                "      \"name\": \"下单数\",\"value\": ");
        rs.append(ts.getOrderCount());
        rs.append(",\"rate_level\": \"custom\",\n" +
                "      \"color\": \"#f05050\"\n" +
                "    },\n" +
                "    {\n" +
                "      \"name\": \"下单金额\",\"value\": ");
        rs.append(ts.getOrderAmount());
        rs.append("}\n" +
                "  ]\n" +
                "}");
        return rs.toString();
    }

    @RequestMapping("/category")
    public String getCategoryOrderDetail(@RequestParam(value = "date",defaultValue = "0") Integer date){
        if (date == 0){
            date = DateFormatUtil.now();
        }
        List<categoryOrderDetail> categoryOrderDetail = tradeStatsService.getCategoryOrderDetail(date);
        com.atguigu.gmall.bean.categoryOrderDetail od = categoryOrderDetail.get(0);

        StringBuilder rs = new StringBuilder("{\n" +
                "  \"status\": 0,\n" +
                "  \"msg\": \"\",\"data\": ");

            rs.append(od.getOrderAmount());

        rs.append("}");

        return rs.toString();
    }

    @RequestMapping("/subject")
    public String getSubjectOrderDetail(@RequestParam(value = "date",defaultValue = "0") Integer date){
        if (date == 0){
            date = DateFormatUtil.now();
        }
        List<subjectOrderDetail> subjectOrderDetail = tradeStatsService.getSubjectOrderDetail(date);

        StringBuilder rs = new StringBuilder("{\"status\": 0,\"msg\": \"\",\"data\": [");
        for (int i = 0; i < subjectOrderDetail.size(); i++) {
            com.atguigu.gmall.bean.subjectOrderDetail od = subjectOrderDetail.get(i);
            rs.append("    {\"name\": \""+od.getSubject()+"\",");
            rs.append("      \"value\": "+od.getOrderAmount()+"}");
            if (i < subjectOrderDetail.size()-1){
                rs.append(",");
            }
        }
        rs.append("  ]}");

        return rs.toString();
    }

    @RequestMapping("/course")
    public String getCourseOrderDetail(@RequestParam(value = "date",defaultValue = "0") Integer date){
        if (date == 0){
            date = DateFormatUtil.now();
        }
        List<courseOrderDetail> courseOrderDetail = tradeStatsService.getCourseOrderDetail(date);


        StringBuilder rs = new StringBuilder("{\"status\": 0,\"msg\": \"\",\"data\": {\"categories\": [");

        for (int i = 0; i < courseOrderDetail.size() ; i++) {
            com.atguigu.gmall.bean.courseOrderDetail od = courseOrderDetail.get(i);
            rs.append("\""+od.getCourse()+"\"");
            if (i < courseOrderDetail.size()-1){
                rs.append(",\n");
            }
        }

        rs.append("],\"series\": [{\"name\": \"手机品牌\",\"data\": [");


        for (int i = 0; i < courseOrderDetail.size() ; i++) {
            com.atguigu.gmall.bean.courseOrderDetail od = courseOrderDetail.get(i);
                rs.append(od.getOrderAmount());
            if (i < courseOrderDetail.size()-1){
                rs.append(",");
            }
        }
        rs.append("        ]}]}}");


        return rs.toString();
    }




}
